package cn.linkey.rulelib.S005;

import java.util.HashMap;

import cn.linkey.dao.Rdb;
import cn.linkey.doc.Document;
import cn.linkey.factory.BeanCtx;
import cn.linkey.rule.LinkeyRule;
import cn.linkey.util.DateUtil;

/**
 * @RuleName:格式化我的已办
 * @author admin
 * @version: 8.0
 * @Created: 2014-05-09 11:59
 */
final public class R_S005_E020 implements LinkeyRule {
    @Override
    public String run(HashMap<String, Object> params) {
        Document configdoc = (Document) params.get("ConfigDoc"); //grid配置文档
        Document doc = (Document) params.get("DataDoc"); //数据主文档
        String eventName = (String) params.get("EventName");//事件名称
        if (eventName.equals("formatRowData")) {
            formatRowData(doc);
        }
        else if (eventName.equals("formatSql")) {
            return formatSql(configdoc);
        }
        else if (eventName.equals("getTotalNum")) {
            return getTotalNum(configdoc);
        }
        else if (eventName.equals("onDataSourceOpen")) {
            onDataSourceOpen(configdoc);
        }
        return "";
    }

    public void onDataSourceOpen(Document configdoc) {
        //通过操作configdoc可以在数据源打开时动态修改配置参数
        //动态修改SqlWhere选择条件: configdoc.s("SqlWhere","id='1001'");
        String sqlWhere = "";
        if (Rdb.getDbType().equals("MSSQL")) {
            sqlWhere = "WF_Status='Current' and ','+WF_EndUser+',' like '%," + BeanCtx.getUserid() + ",%'";
        }
        else if (Rdb.getDbType().equals("MYSQL")) {
            sqlWhere = "WF_Status='Current' and concat(',',WF_EndUser,',') like '%," + BeanCtx.getUserid() + ",%'";
        }
        else {
            sqlWhere = "WF_Status='Current' and ','||WF_EndUser||',' like '%," + BeanCtx.getUserid() + ",%'";
        }
        configdoc.s("SqlWhere", sqlWhere);
    }

    public void formatRowData(Document doc) {
        //doc对像对应数据库表中的记录可修改或获取所有字段
        //格式化列数据如: doc.s("Subject",doc.g("Subject")+doc.g("UserName")); 可重新格式化Subject字段
        
//        String sql = "select WF_ENDTIME from BPM_ARCHIVEDDATA where WF_ORUNID='" + doc.g("WF_OrUnid") + "'";
//        String endTime = "";
        
        String difTime = DateUtil.getAllDifTime(doc.g("WF_DocCreated"), DateUtil.getNow());
        int min = Integer.valueOf(difTime);
        if (min > 60) {
            difTime = String.valueOf(min / 60) + "(小时)";
        }
        else {
            difTime = min + "(分钟)";
        }
        doc.s("TotalTime", difTime);
    }

    public String formatSql(Document configdoc) {
        /*
         * configdoc表示当前json数据源的配置信息 自定义复杂的sql语句 当配置中的where条件无法满足时 可以实现自定义sql String sql="select * from "+configDoc.g("SqlTableName")+" where WF_OrUnid='"+BeanCtx.g("fromurlarg")+"'"
         */
        return ""; //返回空表示使用配置值
    }

    public String getTotalNum(Document configdoc) {
        /* 与formatSql一起使用返回文档总数 */
        return "0";
    }

}